package com.northpool.operator.analysis.dataset;

import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.stpclient.StpClientOperatorBase;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;

import java.math.BigDecimal;
import java.math.RoundingMode;

@OperatorInfo(
        ref = "RatioCalOperator",
        name = "百分比计算",
        description = ""
)
@OutputParams({
        @OutputParam(ref = "ratioResult", name = "计算结果", type = TypeEnum.STRING)
})
public class RatioCalOperator extends StpClientOperatorBase {

    @InputParams({
            @InputParam(ref = "part", name = "分子", type = TypeEnum.NUMBER, required = true, testValue = "1"),
            @InputParam(ref = "total", name = "分母", type = TypeEnum.NUMBER, required = true, testValue = "100"),
            @InputParam(ref = "scale", name = "精度", type = TypeEnum.NUMBER, required = true, testValue = "2"),

    })
    public RatioCalOperator() {
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {

        BigDecimal partBigDecimal = paramTool.getNumber("part");
        BigDecimal totalBigDecimal = paramTool.getNumber("total");
        BigDecimal scale = paramTool.getNumber("scale");

        BigDecimal hundred = new BigDecimal("100");
        int scaleInt = scale.intValue();
        BigDecimal percentage = partBigDecimal.divide(totalBigDecimal, scaleInt, RoundingMode.HALF_UP).multiply(hundred);
        this.result("ratioResult", percentage.toString() + "%");
    }
}
